﻿using Aurora.AutoMapper;
using Aurora.Security.RSA.Util;
using Aurora.SlideCaptcha;
using Aurora.TaskScheduler.Storage.MySql;
using FileServer.Extension;
using Gateway.Application;
using Gateway.Domain.EFCore;
using Gateway.IApplication;
using Gateway.Infrastructure;
using Gateway.Query;
using Gateway.UI.Api.AbpModules;
using Microsoft.Extensions.Caching.Distributed;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Modularity;

namespace Gateway.UI.Api
{
    /// <summary>
    /// 启动 module
    /// </summary>
    [DependsOn(typeof(AbpAspNetCoreMvcModule),
        typeof(KestrelModule),
        typeof(HttpClientModule),
        typeof(TaskSchedulerModule),
        typeof(ApplicationInterfaceAbpModule),
        typeof(ApplicationAbpModule),
        typeof(EFCoreAbpModule),
        typeof(SqlSugarAbpModule),
        typeof(InfrastructureAbpModule),
        typeof(QueryAbpModule),
        typeof(AiEndpointModule)
        // typeof(AuroraSKAbpModule)
    )]
    public class StartupModule : AbpModule
    {
        /// <summary>
        /// service 
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task ConfigureServicesAsync(ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();
            context.Services.Configure<UploadOption>(configuration.GetSection("Upload"));

            #region 注入雪花id

            context.Services.AddSnowFlakeNoGenter();

            #endregion

            context.Services.AddScoped<IRSAUtil, RSAUtil>();
            context.Services.AddHeaderPropagation();
           
            // context.Services.AddScoped<HttpContextMiddleware>();
            context.Services.AddRedis(configuration);

            context.Services.AddHubs(configuration);

            #region add MediatR

            context.Services.AddMyMediatR(Assembly.GetExecutingAssembly());

            #endregion

            context.Services.AddMyCaptcha(configuration);
           

            context.Services.AddFileServer(options =>
            {
                FileServer.Abstractions.Models.Options.FileServerOptions uploadFileOptions = configuration.GetSection("FileOptions").Get<FileServer.Abstractions.Models.Options.FileServerOptions>();
                options.FilePath = uploadFileOptions.FilePath;
                options.FileTmpPath = uploadFileOptions.FileTmpPath;
                options.FileProviderOptions = uploadFileOptions.FileProviderOptions;
                options.UseMySql(storage =>
                {
                    storage.ConnectionString = configuration["ConnectionSetting"];
                    storage.OpenLogger = true;
                });
            });

            // 注入 model mapper
            context.Services.AddMapsterRegister(AppDomain.CurrentDomain.GetAssemblies());
            return base.ConfigureServicesAsync(context);
        }

        /// <summary>
        /// app init
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            var s= app.ApplicationServices.GetService<IDistributedCache>();
            Console.WriteLine(s.GetType().Name);
            app.MapHubs();
            return base.OnApplicationInitializationAsync(context);
        }
    }
}